package protobuf;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
import protobuf.proto.UserProto;

public class MyProtobufServer {

    public static void main(String[] args) throws InterruptedException {

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
        NioEventLoopGroup eventExecutors1 = new NioEventLoopGroup();
        serverBootstrap.group(eventExecutors,eventExecutors1)
                .localAddress(9999)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        //解码器，通过Google Protocol Buffers序列化框架动态的切割接收到的ByteBuf
                        channel.pipeline().addLast(new ProtobufVarint32FrameDecoder());
                        //解码成proto对象
                        channel.pipeline().addLast(new ProtobufDecoder(UserProto.Student.getDefaultInstance()));

                        //Google Protocol Buffers编码器
                        channel.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                        //Google Protocol Buffers编码器
                        channel.pipeline().addLast(new ProtobufEncoder());

                        channel.pipeline().addLast(new SimpleChannelInboundHandler<UserProto.Student>(){

                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                UserProto.Student.Builder msg = UserProto.Student.newBuilder().setName("周杰伦").setMsg("我来了");
                                ctx.writeAndFlush(msg);
                            }

                            @Override
                            protected void channelRead0(ChannelHandlerContext channelHandlerContext, UserProto.Student student) throws Exception {
                                System.out.println(student.getName()+":"+ student.getMsg());
                            }
                        });



                    }
                })
        ;

        ChannelFuture channelFuture = null;
        try {
            channelFuture = serverBootstrap.bind().sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            eventExecutors.shutdownGracefully().sync();
            eventExecutors1.shutdownGracefully().sync();
        }


    }

}
